package org.framework.edge.builder.infrastructure.persistence;

import org.framework.edge.builder.infrastructure.entity.DockerRepositoryBackupDO;
import org.framework.edge.builder.infrastructure.converter.DockerRepositoryBackupConverter;
import org.framework.edge.builder.infrastructure.mapper.DockerRepositoryBackupMapper;
import org.framework.edge.builder.domain.model.docker.repository.backup.DockerRepositoryBackupRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.framework.edge.builder.domain.model.docker.repository.backup.DockerRepositoryBackup;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe Docker仓库备份
 *
 * @author Jia wei Wu
 * @date 2025/07/10 08:18 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class DockerRepositoryBackupRepositoryImpl implements DockerRepositoryBackupRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增Docker仓库备份
     *
     * @param dockerRepositoryBackup 新增Docker仓库备份
     * @return {@link Result<DockerRepositoryBackup>} Docker仓库备份新增后领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<DockerRepositoryBackup> story(DockerRepositoryBackup dockerRepositoryBackup) {
        DockerRepositoryBackupDO dockerRepositoryBackupDO = DockerRepositoryBackupConverter.INSTANCE.fromDockerRepositoryBackup(dockerRepositoryBackup);
        lazyLambdaStream.upsert(dockerRepositoryBackupDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增Docker仓库备份
     *
     * @param dockerRepositoryBackupList 批量新增Docker仓库备份
     * @return {@link Result<List<DockerRepositoryBackup>>} Docker仓库备份新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<List<DockerRepositoryBackup>> batchStory(List<DockerRepositoryBackup> dockerRepositoryBackupList) {
        List<DockerRepositoryBackupDO> dockerRepositoryBackupDOList = dockerRepositoryBackupList.stream().map(DockerRepositoryBackupConverter.INSTANCE::fromDockerRepositoryBackup).collect(Collectors.toList());
        lazyLambdaStream.upsert(dockerRepositoryBackupDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个Docker仓库备份
     *
     * @param dockerRepositoryBackup 查询单个Docker仓库备份
     * @return {@link Result<DockerRepositoryBackup>} Docker仓库备份领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<DockerRepositoryBackup> findOne(DockerRepositoryBackup dockerRepositoryBackup) {
        DockerRepositoryBackupDO dockerRepositoryBackupDO = DockerRepositoryBackupConverter.INSTANCE.fromDockerRepositoryBackup(dockerRepositoryBackup);
        DockerRepositoryBackup dockerRepositoryBackupOne = lazyLambdaStream.selectOne(
                LazyWrappers.<DockerRepositoryBackupDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getBackupImageNum, dockerRepositoryBackupDO.getBackupImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getCreateTime, dockerRepositoryBackupDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getId, dockerRepositoryBackupDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getSourceRepositoryId, dockerRepositoryBackupDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTagRepositoryId, dockerRepositoryBackupDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTotalImageNum, dockerRepositoryBackupDO.getTotalImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getUpdateTime, dockerRepositoryBackupDO.getUpdateTime())
                , DockerRepositoryBackup.class);
        return ResultFactory.successOf(dockerRepositoryBackupOne);
    }

    /**
     * describe 查询多个Docker仓库备份
     *
     * @param dockerRepositoryBackup 查询多个Docker仓库备份
     * @return {@link Result<List<DockerRepositoryBackup>>} Docker仓库备份领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<List<DockerRepositoryBackup>> findList(DockerRepositoryBackup dockerRepositoryBackup) {
        DockerRepositoryBackupDO dockerRepositoryBackupDO = DockerRepositoryBackupConverter.INSTANCE.fromDockerRepositoryBackup(dockerRepositoryBackup);
        List<DockerRepositoryBackup> dockerRepositoryBackupList = lazyLambdaStream.selectList(
                LazyWrappers.<DockerRepositoryBackupDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getBackupImageNum, dockerRepositoryBackupDO.getBackupImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getCreateTime, dockerRepositoryBackupDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getId, dockerRepositoryBackupDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getSourceRepositoryId, dockerRepositoryBackupDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTagRepositoryId, dockerRepositoryBackupDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTotalImageNum, dockerRepositoryBackupDO.getTotalImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getUpdateTime, dockerRepositoryBackupDO.getUpdateTime())
                , DockerRepositoryBackup.class);
        return ResultFactory.successOf(dockerRepositoryBackupList);
    }

    /**
     * describe 分页查询多个Docker仓库备份
     *
     * @param size                   当前页数
     * @param current                当前页
     * @param dockerRepositoryBackup 分页查询多个Docker仓库备份
     * @return {@link Result<LazyPage<DockerRepositoryBackup>>} 分页Docker仓库备份领域对象
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<LazyPage<DockerRepositoryBackup>> findPage(int size, int current, DockerRepositoryBackup dockerRepositoryBackup) {
        DockerRepositoryBackupDO dockerRepositoryBackupDO = DockerRepositoryBackupConverter.INSTANCE.fromDockerRepositoryBackup(dockerRepositoryBackup);
        LazyPage<DockerRepositoryBackup> lazyPage = new LazyPage<>(current, size);
        LazyPage<DockerRepositoryBackup> dockerRepositoryBackupLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<DockerRepositoryBackupDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getBackupImageNum, dockerRepositoryBackupDO.getBackupImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getCreateTime, dockerRepositoryBackupDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getId, dockerRepositoryBackupDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getSourceRepositoryId, dockerRepositoryBackupDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTagRepositoryId, dockerRepositoryBackupDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTotalImageNum, dockerRepositoryBackupDO.getTotalImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getUpdateTime, dockerRepositoryBackupDO.getUpdateTime())
                , lazyPage, DockerRepositoryBackup.class);
        return ResultFactory.successOf(dockerRepositoryBackupLazyPage);
    }

    /**
     * describe 删除Docker仓库备份
     *
     * @param dockerRepositoryBackup 删除Docker仓库备份
     * @return {@link Result<DockerRepositoryBackup>} Docker仓库备份
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<DockerRepositoryBackup> remove(DockerRepositoryBackup dockerRepositoryBackup) {
        DockerRepositoryBackupDO dockerRepositoryBackupDO = DockerRepositoryBackupConverter.INSTANCE.fromDockerRepositoryBackup(dockerRepositoryBackup);
        lazyLambdaStream.delete(
                LazyWrappers.<DockerRepositoryBackupDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getBackupImageNum, dockerRepositoryBackupDO.getBackupImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getCreateTime, dockerRepositoryBackupDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getId, dockerRepositoryBackupDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getSourceRepositoryId, dockerRepositoryBackupDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTagRepositoryId, dockerRepositoryBackupDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTotalImageNum, dockerRepositoryBackupDO.getTotalImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getUpdateTime, dockerRepositoryBackupDO.getUpdateTime())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在Docker仓库备份
     *
     * @param dockerRepositoryBackup Docker仓库备份领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/07/10 08:18 晚上
     **/

    @Override
    public Result<Boolean> exists(DockerRepositoryBackup dockerRepositoryBackup) {
        DockerRepositoryBackupDO dockerRepositoryBackupDO = DockerRepositoryBackupConverter.INSTANCE.fromDockerRepositoryBackup(dockerRepositoryBackup);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<DockerRepositoryBackupDO>lambdaWrapper()
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getBackupImageNum, dockerRepositoryBackupDO.getBackupImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getCreateTime, dockerRepositoryBackupDO.getCreateTime())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getId, dockerRepositoryBackupDO.getId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getIsDeleted, false)
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getSourceRepositoryId, dockerRepositoryBackupDO.getSourceRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTagRepositoryId, dockerRepositoryBackupDO.getTagRepositoryId())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getTotalImageNum, dockerRepositoryBackupDO.getTotalImageNum())
                        .eqIgnoreEmpty(DockerRepositoryBackupDO::getUpdateTime, dockerRepositoryBackupDO.getUpdateTime())
        );
        return ResultFactory.successOf(exists);
    }

}